package com.xh.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.ssm.bean.Userinfo;
import com.xh.ssm.bean.UserinfoImage;
import com.xh.ssm.bean.UserinfoProject;
import com.xh.ssm.mapper.UserinfoImageMapper;
import com.xh.ssm.mapper.UserinfoMapper;
import com.xh.ssm.mapper.UserinfoProjectMapper;
import com.xh.ssm.service.UserinfoService;
import com.xh.ssm.util.R;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/1/26/026
 */
@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {

    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private UserinfoProjectMapper userinfoProjectMapper;
    @Autowired
    private UserinfoImageMapper userinfoImageMapper;

    public Userinfo login(Userinfo userinfo) {
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        return userinfoMapper.login(userinfo);
    }

    public R getPage(int page, int limit, Userinfo userinfo) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = userinfoMapper.getList(userinfo);
        PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public Userinfo getById(int userinfoId) {
        return userinfoMapper.selectByPrimaryKey(userinfoId);
    }

    public void saveOrUpdate(Userinfo userinfo) {
        if (userinfo.getId() == null) {
            userinfo.setPassword(DigestUtils.md5Hex("123456"));
            userinfoMapper.insertSelective(userinfo);
        } else {
            if (StringUtils.isNotEmpty(userinfo.getPassword())) {
                userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            }
            userinfoMapper.updateByPrimaryKeySelective(userinfo);
        }
        if (userinfo.getImageUrls() != null && userinfo.getImageUrls().length > 0) {
            userinfoImageMapper.deleteByUserinfoId(userinfo.getId());
            List<UserinfoImage> uiList = new ArrayList<UserinfoImage>();
            for (String url : userinfo.getImageUrls()) {
                UserinfoImage ui = new UserinfoImage();
                ui.setUserinfoId(userinfo.getId());
                ui.setPhoto(url);
                uiList.add(ui);
            }
            userinfoImageMapper.batchInsert(uiList);
        }
        if (userinfo.getProjectIds() != null && userinfo.getProjectIds().length > 0) {
            userinfoProjectMapper.deleteByUserinfoId(userinfo.getId());
            List<UserinfoProject> upList = new ArrayList<UserinfoProject>();
            for (Integer pid : userinfo.getProjectIds()) {
                UserinfoProject up = new UserinfoProject();
                up.setProjectId(pid);
                up.setUserinfoId(userinfo.getId());
                upList.add(up);
            }
            userinfoProjectMapper.batchInsert(upList);
        }
    }

    public void deleteById(int userinfoId) {
        userinfoMapper.deleteByPrimaryKey(userinfoId);
        userinfoProjectMapper.deleteByUserinfoId(userinfoId);
        userinfoImageMapper.deleteByUserinfoId(userinfoId);
    }

    public R checkPassword(Userinfo userinfo) {
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        List<Userinfo> list = userinfoMapper.getList(userinfo);
        return list.isEmpty() ? R.error("原密码不正确！") : R.ok();
    }

    public R getImageList(int userinfoId) {
        return R.ok(userinfoImageMapper.getListByUserinfoId(userinfoId));
    }

    public R getProjectList(int userinfoId) {
        return R.ok(userinfoProjectMapper.getListByUserinfoId(userinfoId));
    }
}
